package com.kingschan.jeasytag.core.parse;
import java.lang.reflect.Field;
import com.kingschan.jeasytag.core.annotations.tag.*;
import com.kingschan.jeasytag.tag.RootTag;
import com.kingschan.jeasytag.tag.power.DIYTag;


/**
 * TldParse
 * @author kingschan
 *
 */
public class TldParse {

	public static String parseFiled(Field[] fields,RootTag tag) throws Exception{
		StringBuffer sb = new StringBuffer();
		for (int j=0;j<fields.length;j++) {
			Field f =fields[j];
			if (f.getName()!="serialVersionUID"&&f.getAnnotations().length>0) {
				String tagstr="<attribute><description>${description}</description><name>${name}</name><rtexprvalue>true</rtexprvalue></attribute>";
				//PropertyDescriptor p = new PropertyDescriptor(f.getName(), tag.getClass());
				//Object value =p.getReadMethod().invoke(tag);
								
				if (f.getAnnotation(BooleanField.class) != null) {
					sb.append(tagstr.replace("${description}",f.getAnnotation(BooleanField.class).description()).replace("${name}", f.getName()));
					
				}
				else if (f.getAnnotation(NumberField.class) != null) {
					sb.append(tagstr.replace("${description}",f.getAnnotation(NumberField.class).description()).replace("${name}", f.getName()));
				}
				else if (f.getAnnotation(JsonField.class) != null) {
					sb.append(tagstr.replace("${description}", f.getAnnotation(JsonField.class).description()).replace("${name}", f.getName()));
				}else if (f.getAnnotation(JsonArrayField.class) != null) {
					sb.append(tagstr.replace("${description}", f.getAnnotation(JsonArrayField.class).description()).replace("${name}", f.getName()));
				}else if (f.getAnnotation(StringField.class) != null) {
					sb.append(tagstr.replace("${description}", f.getAnnotation(StringField.class).description()).replace("${name}", f.getName()));
				}else if (f.getAnnotation(EventField.class) != null) {
					EventField e = f.getAnnotation(EventField.class);
					sb.append(tagstr.replace("${description}", e.description()).replace("${name}", f.getName()));
				}else if (f.getAnnotation(IntArrayField.class) != null) {					
					sb.append(tagstr.replace("${description}", f.getAnnotation(IntArrayField.class).description()).replace("${name}", f.getName()));
				}				
				
			}
		}
		return sb.toString();
	}
	
	public static void builderTLD(RootTag tag) throws Exception{		
		Field[] fields=tag.getClass().getDeclaredFields();
		Field[] superFields = tag.getClass().getSuperclass().getDeclaredFields();
		StringBuffer sb = new StringBuffer();
		sb.append("<tag>");
		sb.append("<name>"+tag.getClass().getSimpleName()+"</name>");
		sb.append("<tag-class>"+tag.getClass().getName()+"</tag-class>");
		sb.append("<body-content>JSP</body-content>");
		sb.append("<display-name>"+tag.getClass().getSimpleName()+"</display-name>");
		//sb.append("<dynamic-attributes>true</dynamic-attributes>");
		sb.append(TldParse.parseFiled(superFields, tag));
		sb.append(TldParse.parseFiled(fields, tag));
		sb.append("</tag>");
		System.out.println(sb.toString());
		
		
	}
	public static void main(String[] args) throws Exception{
		TldParse.builderTLD(new DIYTag());
	}
}
